package com.yunxi.service.urm.dubbo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.baseDataToHub.pojo.urm.*;
import com.yunxi.baseDataToHub.service.urm.UrmDubboService;
import com.yunxi.core.base.Constant;
import com.yunxi.core.base.JsonUtils;
import com.yunxi.core.model.ValidateUtil;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.app.pojo.MobileAccountVO;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import com.yunxi.service.ReturnResultMsg;
import com.yunxi.service.eai.EaiLogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.*;

/**
 * Created by Administrator on 2019/8/5.
 */
public class UrmDubboServiceImpl implements UrmDubboService {
    protected static final Logger LOGGER = LoggerFactory.getLogger(UrmDubboServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Value("${esb.switch}")
    private String esbSwitch;
    @Value("${URM.URL}")
    private String URM_URL;
    @Value("${URM.URL.ACCOUNT}")
    private String URM_URL_ACCOUNT;
    @Value("${COMM.URL}")
    private String COMM_URL;
    @Autowired
    @Qualifier("eaiLogServiceImpl")
    EaiLogService eaiLogService;

    @Autowired
    private Validator validator;


    @Override
    public ApiResult sendClientInfoPMS2Urm(ClientInfoVO order) {
        String orderStatus = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(orderStatus);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送客户信息给URM系统");
        eaiLog.setSourceSystem(Constant.PMS);
        if (order.getOptionType()==1){
            eaiLog.setServiceName(Constant.PMS_SAVE_CLIENT_INFO);
        }else {
            eaiLog.setServiceName(Constant.PMS_UPDATE_CLIENT_INFO);
        }
        if (esbSwitch.equals("false")) {
            eaiLog.setCreateDate(new Date());
            eaiLogService.saveOrUpdate(eaiLog);
            return ResponseUtil.ok();
        }
        try{
            Map map=null;
            if (order.getOptionType()==1){
               map = HttpUtil.sendPost(orderStatus, Constant.PMS_SAVE_CLIENT_INFO, Constant.PMS, URM_URL);
            }else {
               map = HttpUtil.sendPost(orderStatus, Constant.PMS_UPDATE_CLIENT_INFO, Constant.PMS, URM_URL);
            }
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(URM_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(Constant.URM_R_CODE_FAIL,"["+Constant.PMS_UPDATE_CLIENT_INFO+"]");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (Integer.parseInt(String.valueOf(jsonResponse.get("code")))!=200){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult = ResponseUtil.fail(Constant.URM_R_CODE_ERR,"["+Constant.PMS_UPDATE_CLIENT_INFO+"]"+jsonResponse.get("message"));
                }else {
                    apiResult = ResponseUtil.ok(jsonResponse.get("data"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EXP,"["+Constant.PMS_UPDATE_CLIENT_INFO+"]");
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EAI,"["+Constant.PMS_UPDATE_CLIENT_INFO+"]");
        }
        return apiResult;
    }

    @Override
    public ApiResult selectAccountByMobilePMS2Urm(UserInfoVO userInfoVO) {
      /*    Map dataMap=new HashMap();
      dataMap.put("mobile",mobile);
        String maps = JSONObject.toJSONString(dataMap);*/

        ValidateUtil<UserInfoVO> v = new ValidateUtil<UserInfoVO>();
        String valiStr = v.validate(userInfoVO);
        if(valiStr!=null) return ResponseUtil.fail(502,valiStr);

        Set<ConstraintViolation<UserInfoVO>> violations = validator.validate(userInfoVO);
        if (!violations.isEmpty()) {
            Iterator<ConstraintViolation<UserInfoVO>> it = violations.iterator();
            while (it.hasNext()) {
                ConstraintViolation str = it.next();
                System.out.println(str.getMessage());
            }
        }

        String userInfo = JSONObject.toJSONString(userInfoVO);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(userInfo);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统通过手机号获取用户信息");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_ACCOUNT_BY_MOBILE);
        if (esbSwitch.equals("false")) {
            eaiLog.setCreateDate(new Date());
            eaiLogService.saveOrUpdate(eaiLog);
            return ResponseUtil.ok(ReturnResultMsg.selectAccountByMobilePMS2Urm());
        }
        try{
            Map map = HttpUtil.sendPost(userInfo, Constant.PMS_ACCOUNT_BY_MOBILE, Constant.PMS, URM_URL_ACCOUNT);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(URM_URL_ACCOUNT);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(Constant.URM_R_CODE_FAIL,"["+Constant.PMS_ACCOUNT_BY_MOBILE+"]");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (!String.valueOf(jsonResponse.get("code")).equals("200")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(Constant.URM_R_CODE_ERR,"["+Constant.PMS_ACCOUNT_BY_MOBILE+"]"+jsonResponse.get("message"));
                }else {
                    MobileAccountVO mobileAccountVO= JsonUtils.jsonToPojo(String.valueOf(jsonResponse.get("data")),MobileAccountVO.class);
                    if (StringUtils.isNotEmpty(mobileAccountVO.getRegionCode())){
                        String region=mobileAccountVO.getRegionCode();
                        if (region.length()==2){
                            mobileAccountVO.setRegProvince(region);
                        }else if (region.length()==4){
                            mobileAccountVO.setRegProvince(region.substring(0,2));
                            mobileAccountVO.setRegCity(region);
                        }else if (region.length()==6){
                            mobileAccountVO.setRegProvince(region.substring(0,2));
                            mobileAccountVO.setRegCity(region.substring(0,4));
                            mobileAccountVO.setRegStrict(region);
                        }
                    }
                    apiResult = ResponseUtil.ok(JSONObject.toJSONString(mobileAccountVO));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EXP,"["+Constant.PMS_ACCOUNT_BY_MOBILE+"]");
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EAI,"["+Constant.PMS_ACCOUNT_BY_MOBILE+"]");
        }
        return apiResult;
    }

    @Override
    public ApiResult sendFollowInformationPMS2Urm(FollowInformationVO order) {
        String data = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(data);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送跟进数据给URM");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_FOLLEW_INFO);
        if (esbSwitch.equals("false")) {
            eaiLog.setCreateDate(new Date());
            eaiLogService.saveOrUpdate(eaiLog);
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(data, Constant.PMS_FOLLEW_INFO, Constant.PMS, URM_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(URM_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(Constant.URM_R_CODE_FAIL,"["+Constant.PMS_FOLLEW_INFO+"]");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (!String.valueOf(jsonResponse.get("code")).equals("200")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(Constant.URM_R_CODE_ERR,"["+Constant.PMS_FOLLEW_INFO+"]"+jsonResponse.get("message"));
                }else {
                        apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EXP,"["+Constant.PMS_FOLLEW_INFO+"]");
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EAI,"["+Constant.PMS_FOLLEW_INFO+"]");
        }
        return apiResult;
    }

    @Override
    public ApiResult sendIntentionalInformationPMS2Urm(IntentionalInformationVO order) {
        String data = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(data);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送意向信息给URM");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_INTENTIONAL_INFO);
        if (esbSwitch.equals("false")) {
            eaiLog.setCreateDate(new Date());
            eaiLogService.saveOrUpdate(eaiLog);
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(data, Constant.PMS_INTENTIONAL_INFO, Constant.PMS, URM_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(URM_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(Constant.URM_R_CODE_FAIL,"["+Constant.PMS_INTENTIONAL_INFO+"]");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (!String.valueOf(jsonResponse.get("code")).equals("200")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(Constant.URM_R_CODE_ERR,"["+Constant.PMS_INTENTIONAL_INFO+"]"+jsonResponse.get("message"));
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EXP,"["+Constant.PMS_INTENTIONAL_INFO+"]");
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EAI,"["+Constant.PMS_INTENTIONAL_INFO+"]");
        }
        return apiResult;
    }

    @Override
    public ApiResult sendOrderInformationTransmissionPMS2Urm(OrderInformationTransmissionVO order) {
        String data = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(data);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送订单信息给URM");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_ORDER_INFO_TRANSFER);
        if (esbSwitch.equals("true")) {
            eaiLog.setCreateDate(new Date());
            eaiLogService.saveOrUpdate(eaiLog);
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(data, Constant.PMS_ORDER_INFO_TRANSFER, Constant.PMS, URM_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(URM_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(Constant.URM_R_CODE_FAIL,"["+Constant.PMS_ORDER_INFO_TRANSFER+"]");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (!String.valueOf(jsonResponse.get("code")).equals("200")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(Constant.URM_R_CODE_ERR,"["+Constant.PMS_ORDER_INFO_TRANSFER+"]"+jsonResponse.get("message"));
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EXP,"["+Constant.PMS_ORDER_INFO_TRANSFER+"]");
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EAI,"["+Constant.PMS_ORDER_INFO_TRANSFER+"]");
        }
        return apiResult;
    }

    @Override
    public ApiResult sendUrmTestDrivePMS2Urm(UrmTestDriveVO order) {
        String data = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(data);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送试乘试驾信息给URM");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_URM_TEST_DRIVE);
        if (esbSwitch.equals("false")) {
            eaiLog.setCreateDate(new Date());
            eaiLogService.saveOrUpdate(eaiLog);
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(data, Constant.PMS_URM_TEST_DRIVE, Constant.PMS, COMM_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(COMM_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(Constant.URM_R_CODE_FAIL,"["+Constant.PMS_URM_TEST_DRIVE+"]");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (!String.valueOf(jsonResponse.get("esbCode")).equals("000000")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(Constant.URM_R_CODE_ERR,"["+Constant.PMS_URM_TEST_DRIVE+"]"+jsonResponse.get("message"));
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EXP,"["+Constant.PMS_URM_TEST_DRIVE+"]");
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EAI,"["+Constant.PMS_URM_TEST_DRIVE+"]");
        }
        return apiResult;
    }

    @Override
    public ApiResult sendDeliveryVehiclePMS2Urm(DeliveryVehicleVO order) {
        String data = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(data);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送交车信息给URM");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_DELIVERY_VEHICLE);
        if (esbSwitch.equals("false")) {
            eaiLog.setCreateDate(new Date());
            eaiLogService.saveOrUpdate(eaiLog);
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(data, Constant.PMS_DELIVERY_VEHICLE, Constant.PMS, URM_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(URM_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(Constant.URM_R_CODE_FAIL,"["+Constant.PMS_DELIVERY_VEHICLE+"]");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (!String.valueOf(jsonResponse.get("code")).equals("200")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(Constant.URM_R_CODE_ERR,"["+Constant.PMS_DELIVERY_VEHICLE+"]"+jsonResponse.get("message"));
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EXP,"["+Constant.PMS_DELIVERY_VEHICLE+"]");
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EAI,"["+Constant.PMS_DELIVERY_VEHICLE+"]");
        }
        return apiResult;
    }

    /**
     * 推送交车信息
     * @param urmHandleVehicleVO
     * @return
     */
    @Override
    public ApiResult sendHandleVehiclePMS2Urm(UrmHandleVehicleVO urmHandleVehicleVO) {
        String data = JSONObject.toJSONString(urmHandleVehicleVO);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(data);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送交车信息给URM");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_DELIVERY_VEHICLE);
        if (esbSwitch.equals("false")) {
            eaiLog.setCreateDate(new Date());
            eaiLogService.saveOrUpdate(eaiLog);
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(data, Constant.PMS_DELIVERY_VEHICLE, Constant.PMS, URM_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(URM_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(Constant.URM_R_CODE_FAIL,"["+Constant.PMS_DELIVERY_VEHICLE+"]");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (!String.valueOf(jsonResponse.get("code")).equals("200")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(Constant.URM_R_CODE_ERR,"["+Constant.PMS_DELIVERY_VEHICLE+"]"+jsonResponse.get("message"));
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EXP,"["+Constant.PMS_DELIVERY_VEHICLE+"]");
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail(Constant.URM_R_CODE_EAI,"["+Constant.PMS_DELIVERY_VEHICLE+"]");
        }
        return apiResult;
    }
}
